import java.util.ArrayList;
import javafx.animation.Interpolator;
import javafx.animation.ScaleTransition;
import javafx.scene.effect.DropShadow;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.AnchorPane;
import javafx.scene.paint.Color;
import javafx.scene.text.Text;
import javafx.util.Duration;


public class WasteDeck extends AnchorPane{
	private  ArrayList<Card> cards = new ArrayList<Card>();
	private WasteArea delegator;
	public int color;
	private int delta;
	private boolean isBack;
	private Text number = new Text();
	private Text score = new Text();
	private ImageView back = new ImageView();
	
	public void setDelegate(WasteArea g){
		delegator = g;
	}
	
	private void init(){
		
		this.setPrefSize(80, 100);
		this.getChildren().addAll(number,score,back);
		
		back.setImage(new Image("placeHold.jpg",90,135,true,true));
		back.setX(0);
		back.setY(0);
		number.setX(30);
		number.setY(165);
		
		score.setX(30);
		score.setY(150);
		delta=-20;
		isBack=false;
		 DropShadow shadow = new DropShadow();
		 shadow.setOffsetX(0.4);
		 shadow.setOffsetY(0.4);
		 shadow.setColor(Color.ORANGE.brighter());
	 	 this.setEffect(shadow);
	 	 
	 	 this.setOnMouseClicked(e->{
	 		 if(isBack){
	 			 turnAllUp();
	 		 }
	 		 else{
	 			 turnAllBack();
	 		 }
	 			 
	 	 });
	}
	
	public WasteDeck(){
		this.init();
	}
	
	
	//初始化设置每个堆的颜色
	public WasteDeck(int col){	
		try{
			if(col>=1 && col<=6){
				color = col;
				this.init();
			}
			else{
				throw new Exception();
			}
		}
		catch(Exception ex){
			System.out.println("Waste Deck:wrong color");
		}
	}
	
	
	public void addCard(Card cd){
		try{
			if(cd.color == color){
				cards.add(cd);
				this.getChildren().add(cd);
				if(isBack){
					cd.turnBack();
				}
				else{
					cd.turnUp();
				}
				delta +=20;
				AnchorPane.setTopAnchor(cd, -(double) delta);
				//AnchorPane.setLeftAnchor(cd, (double) delta);
				
				ScaleTransition smaller = new ScaleTransition();
				smaller.setNode(cd);
				smaller.setFromX(1.5);
				smaller.setFromY(1.5);
				smaller.setToX(1.0);
				smaller.setToY(1.0);
				smaller.setDuration(Duration.millis(1000));
				smaller.setInterpolator(Interpolator.EASE_IN);
				smaller.play();
				smaller.setOnFinished(e->{
					this.setLabel();
					delegator.wasteDeckAddFinished();
				});
				
			}
			else{
				throw new Exception();
			}
		}
		catch(Exception ex){
			System.out.println("WasteDeck:added card' color different from the deck's color");
		}
	}
		
	
	public int count(){
		return cards.size();
	}
	
	private void setLabel(){
		if(this.count()!=0 || this.score() !=0){
			number.setText("count:"+this.count());
			score.setText("score:"+this.score());
		}
	}
	
	public int score(){
		int ret=0;
		for(Card cd:cards){
			System.out.println("wasteDeck:turned " + cd.isTurnedBack() );

			if(cd.isTurnedBack()){//翻面的只加1
				ret ++;
				System.out.println("wasteDeck:add one score");
			}
			else{
				ret +=cd.number;
				System.out.println("wasteDeck:add " + cd.number + " score");

			}
		}
		return ret;
	}
	
	public void turnAllBack(){
		if(isBack==false){
			for(Card cd:cards){
				cd.turnBack();
			}
			this.setLabel();
			isBack=true;
		}
	}
	
	public void turnAllUp(){
		if(isBack==true){
			for(Card cd:cards){
				cd.turnUp();
			}
			this.setLabel();
			isBack=false;
		}
	}
	
	public void clear(){
		this.getChildren().clear();
		cards.clear();
		this.init();

		this.number.setText("");
		this.score.setText("");
	}
	
	public int size(){
		return cards.size();
	}
}
